<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>科室管理系统</title>
    <link rel="stylesheet" href="<?= BASE_URL ?>src/assets/css/bootstrap_5.3.2.min.css">
    <link rel="stylesheet" href="<?= BASE_URL ?>src/assets/css/style.css">
</head>
<body>
<!-- 侧边栏 -->
<div class="sidebar">
    <a href="<?= BASE_URL ?>index.php" class="active">系统首页</a>
    <a href="<?= BASE_URL ?>index.php?c=department&a=index">科室信息</a>
    <a href="<?= BASE_URL ?>index.php?c=notice&a=index">公告管理</a>
    <a href="<?= BASE_URL ?>index.php?c=schedule&a=index">医生排班</a>
    <a href="<?= BASE_URL ?>index.php?c=manager&a=index">管理员信息</a>
    <a href="<?= BASE_URL ?>index.php?c=statistics&a=index">统计报表</a>
</div>

<!-- 主内容区域 -->
<div class="main-content">
    <!-- 顶栏 -->
    <div class="topbar">
        <h5 class="mb-0">科室信息</h5>
        <div class="topbar-right">
            <a href="<?= BASE_URL ?>index.php?c=login&a=logout" class="btn btn-danger logout-btn">退出登录</a>
        </div>
    </div>

    <!--  新增  -->
    <a href="<?= BASE_URL ?>index.php?c=department&a=add" class="btn btn-primary mb-3">新增科室</a>

    <!-- 搜索 -->
    <form method="GET" action="<?= BASE_URL ?>index.php" class="mt-4">
        <input type="hidden" name="c" value="department">
        <input type="hidden" name="a" value="index">
        <div class="input-group mb-3">
            <input type="text" class="form-control" name="search"
                   placeholder="请输入科室名称查询"
                   value="<?= htmlspecialchars($_GET['search'] ?? ''); ?>">
            <button class="btn btn-primary" type="submit">查询</button>
        </div>
    </form>

    <!--  数据表格  -->
    <table class="table table-bordered">
        <thead>
        <tr>
            <th scope="col">科室名称</th>
            <th scope="col">科室描述</th>
            <th scope="col">操作</th>
        </tr>
        </thead>
        <tbody>
        <?php if (!empty($departments)): ?>
            <?php foreach ($departments as $department): ?>
                <tr>
                    <td><?= htmlspecialchars($department['DepartmentName']); ?></td>
                    <td><?= htmlspecialchars($department['DepartmentDescription']); ?></td>
                    <td>
                        <a href="<?= BASE_URL ?>index.php?c=department&a=edit&id=<?= $department['id']; ?>"
                           class="btn btn-sm btn-info">编辑</a>
                        <a href="<?= BASE_URL ?>index.php?c=department&a=delete&id=<?= $department['id']; ?>"
                           class="btn btn-sm btn-danger"
                           onclick="return confirm('确定删除？')">删除</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="3" class="text-center">暂无数据</td>
            </tr>
        <?php endif; ?>
        </tbody>


    </table>
    <!-- 分页导航 -->
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">
            <?php
            $currentPage = $_GET['page']??1;
            $searchQuery = isset($_GET['search']) ? urlencode($_GET['search']):'';

            for ($i = 1; $i <= $total_pages; $i++): ?>
                <li class="page-item <?= $i == $currentPage ? 'active' : '' ?>">
                    <a class="page-link"
                       href="<?= BASE_URL?>index.php?c=department&a=index&page=<?=$i?>&search=<?= $searchQuery ?>">
                        <?= $i ?>
                    </a>
                </li>
            <?php endfor; ?>
        </ul>
    </nav>
</div>

<script src="<?= BASE_URL?><?= BASE_URL ?>src/assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>